package iot

import (
	"errors"
	"time"

	http_label_center "gobase/http_work_flow/label_center"
)

const (
	AlarmType车辆驶入    = 111
	AlarmType车辆驶出    = 112
	AlarmType烟火检测    = 8
	AlarmType物品遗失    = 19
	AlarmType区域入侵    = 20
	AlarmType车辆排队    = 132
	AlarmType违规放置灭火器 = 146
)

type Info struct {
	Code     int    `json:"code"`
	DeviceID string `json:"deviceId"`
	Mid      int    `json:"mid"`
	Param    struct {
		File struct {
			Downloadurl string `json:"downloadurl"`
			Name        string `json:"name"`
		} `json:"file"`
		Filetype string `json:"filetype"`
	} `json:"param"`
	Timestamp string `json:"timestamp"`
	Type      string `json:"type"`
}

type CarTextIot struct {
	Text string `json:"text"`
}

// 车辆驶入驶出上报结果示例
type ReqChargeRecDeviceTag struct {
	DeviceId string `json:"DeviceId"`
	IdScheme int    `json:"IdScheme"` //方案ID

	TimeStart time.Time `json:"TimeStart"`
	TimeEnd   time.Time `json:"TimeEnd"`

	JsonDeviceUpload string                `json:"JsonDeviceUpload"`
	Tag              http_label_center.Tag `json:"Tag"`
}

// 车辆驶入驶出上报结果示例
type CarParkSpace struct {
	ParkSpaceResults []ParkSpaceResult `json:"ParkSpaceResults"`
}

type ParkSpaceResult struct {
	DeviceId string `json:"DeviceId,omitempty"` //国标ID

	DeviceName       string `json:"DeviceName"`
	DeviceAddr       string `json:"DeviceAddr"`
	SerialNumber     string `json:"SerialNumber"`
	IPCSerialNum     string `json:"IPCSerialNum"`
	IPCAddr          string `json:"IPCAddr"`
	ChannelID        string `json:"ChannelId"`
	ChannelName      string `json:"ChannelName"`
	ChannelNum       string `json:"ChannelNum"`
	CapType          int    `json:"CapType"`
	AlarmType        int    `json:"AlarmType"`
	CapID            int    `json:"CapId"`
	DriveIntoStatus  int    `json:"DriveIntoStatus"`
	AreaID           string `json:"AreaId"`
	Timestamp        int64  `json:"Timestamp"`
	BigImageURL      string `json:"BigImageURL"`
	PlateImageURL    string `json:"PlateImageURL"`
	LeftTopX         int    `json:"LeftTopX"`
	LeftTopY         int    `json:"LeftTopY"`
	RightBtmX        int    `json:"RightBtmX"`
	RightBtmY        int    `json:"RightBtmY"`
	PlateNo          string `json:"PlateNO"`
	PlateReliability int    `json:"PlateReliability"`
	PlateColor       int    `json:"PlateColor"`
	PlateType        int    `json:"PlateType"`
	RealAlarm        int    `json:"RealAlarm"`
}

// 车辆驶入驶出上报结果示例
type CarBehavior struct {
	BehaviorResults []BehaviorResult `json:"BehaviorResults"`
}

// 行为分析上报结果示例
type BehaviorResult struct {
	DeviceId string `json:"DeviceId,omitempty"` //国标ID

	DeviceName   string `json:"DeviceName"`
	SerialNumber string `json:"SerialNumber"`
	DeviceAddr   string `json:"DeviceAddr"`
	DeviceAddr0  string `json:"DeviceAddr0"`
	DeviceAddr1  string `json:"DeviceAddr1"`
	ChannelID    string `json:"ChannelId"`
	IPCAddr      string `json:"IPCAddr"`
	ChannelName  string `json:"ChannelName"`
	ChannelNum   string `json:"ChannelNum"`
	IPCSerialNum string `json:"IPCSerialNum"`
	AlarmType    int    `json:"AlarmType"`
	AlarmID      int    `json:"AlarmId"`
	ReportRate   int    `json:"ReportRate"`
	Timestamp    int64  `json:"Timestamp"`
	BigImageURL  string `json:"BigImageURL"`
	BigImage     struct {
		ImageType string `json:"ImageType"`
		Image     string `json:"Image"`
	} `json:"BigImage"`
	PersonNum      int    `json:"PersonNum"`
	EventNum       int    `json:"EventNum"`
	AreaID         string `json:"AreaId"`
	AlarmVideoName string `json:"AlarmVideoName"`
	AlarmVideoURL  string `json:"AlarmVideoURL"`
	LeftTopX       int    `json:"LeftTopX"`
	LeftTopY       int    `json:"LeftTopY"`
	RightBtmX      int    `json:"RightBtmX"`
	RightBtmY      int    `json:"RightBtmY"`
	PlateNo        string `json:"PlateNO"`
}

// Struck2Draw 数据转绘制
func Struck2DrawParkSpace(value ParkSpaceResult, typeId int) (_frame http_label_center.Tag, _err error) {
	if typeId != 0 && typeId != value.AlarmType {
		_err = errors.New("类型参数有误")
		return
	}
	var pens = []http_label_center.Pen{
		{X: int64(value.LeftTopX), Y: int64(value.LeftTopY)},
		{X: int64(value.RightBtmX), Y: int64(value.LeftTopY)},
		{X: int64(value.RightBtmX), Y: int64(value.RightBtmY)},
		{X: int64(value.LeftTopX), Y: int64(value.RightBtmY)},
	}
	var meta = http_label_center.Meta{
		X:      int64(value.LeftTopX),
		Y:      int64(value.LeftTopY),
		Remark: "车辆驶入驶出",
		// Remark:      fmt.Sprintf("车牌:%s", value.PlateNo),
		TypeId:      value.AlarmType,
		ProtoWidth:  10000,
		ProtoHeight: 10000,
		Pen:         &pens,
	}
	var metas = []http_label_center.Meta{meta}
	_frame = http_label_center.Tag{
		Metas: metas,
		AbsoluteTime: &http_label_center.AbsoluteTime{
			CenterUnixMs: value.Timestamp * 1000,
			DurationMs:   3000,
		},
	}
	return
}

// Struck2Draw 数据转绘制
func Struck2DrawBehavior(value BehaviorResult, typeId int) (_frame http_label_center.Tag, _err error) {
	if typeId != 0 && typeId != value.AlarmType {
		_err = errors.New("类型参数有误")
		return
	}
	var pens = []http_label_center.Pen{
		{X: int64(value.LeftTopX), Y: int64(value.LeftTopY)},
		{X: int64(value.RightBtmX), Y: int64(value.LeftTopY)},
		{X: int64(value.RightBtmX), Y: int64(value.RightBtmY)},
		{X: int64(value.LeftTopX), Y: int64(value.RightBtmY)},
	}
	var meta = http_label_center.Meta{
		X:      int64(value.LeftTopX),
		Y:      int64(value.LeftTopY),
		Remark: "行为数据",
		// Remark:      fmt.Sprintf("车牌:%s", value.PlateNo),
		TypeId:      value.AlarmType,
		ProtoWidth:  10000,
		ProtoHeight: 10000,
		Pen:         &pens,
	}
	var metas = []http_label_center.Meta{meta}
	_frame = http_label_center.Tag{
		Metas: metas,
		AbsoluteTime: &http_label_center.AbsoluteTime{
			CenterUnixMs: value.Timestamp * 1000,
			DurationMs:   3000,
		},
	}
	return
}
